//正则验证手机
export const isMobile = (s) => {
	return /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(s)
}


export const unixTimeToDateTime = (unixtime) => {
	let now = new Date(unixtime); // 依情况进行更改 * 1
	let y = now.getFullYear();
	let m = now.getMonth() + 1;
	let d = now.getDate();
	return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
}
/**
 * 数组排序
 * @param {*} arr 需要排序的数组
 * @param {*} key 排序的属性支持嵌套
 * @param {*} index 排序顺序 1升序 2降序
 */
export const ArraySort = (arr, key, index) => {
	let keys = key.split(".")
	let list;
	if (keys.length == 1) {
		list = arr.sort(function(a, b) {
			return a[keys[0]] > b[keys[0]] ? 1 : -1;
		})
	} else if (keys.length == 2) {
		list = arr.sort(function(a, b) {
			return a[keys[0]][keys[1]] > b[keys[0]][keys[1]] ? 1 : -1;
		})
	} else {
		list = arr.sort(function(a, b) {
			return a[keys[0]][keys[1]][keys[2]] > b[keys[0]][keys[1]][keys[2]] ? 1 : -1;
		})
	}
	if (index == 1) {
		return list
	} else {
		return list.reverse()
	}
}

/**
 * 传入起止时间返回中间的日期
 * @param {*} startTime
 * @param {*} endTime
 * @param {*} type 时间类型 1按日返回小时 2按月返回每天 3按日返回小时
 */
export const getScopeTime = (startTime, endTime, type) => {
	let start = new Date(startTime).getTime()
	console.log(start,type)
	let end = new Date(endTime).getTime()
	let time = []
	if (type == 2) {
		for (let i = 0; i < 1; i--) {
			start += 86400000
			if (start == end) {
				time.unshift(startTime.split(' ')[0])
				break
			} else {
				time.push(unixTimeToDateTime(start).split(' ')[0])
			}
		}
	} else if (type == 1) {
		for (let i = 0; i < 1; i--) {
			start += 3600000
			if (start == end) {
				time.unshift(startTime.split(' ')[0])
				break
			} else {
				time.push(unixTimeToDateTime(start))
			}
		}
	}else if (type == 3) {
		for (let i = 0; i < 1; i--) {
			start += 1000*600
			if (start == end) {
				time.unshift(startTime.split(' ')[0])
				break
			} else {
				time.push(unixTimeToDateTime(start))
			}
		}
	}

	return time
}

/**
 * base64转图片对象
 */
export const dataURLtoBlob = (dataurl, name) => {
	let arr = dataurl.split(","),
		mime = arr[0].match(/:(.*?);/)[1],
		bstr = atob(arr[1]),
		n = bstr.length,
		u8arr = new Uint8Array(n);
	while (n--) {
		u8arr[n] = bstr.charCodeAt(n);
	}
	return new File([u8arr], name, {
		type: mime
	});
}

/**
 * 通过url下载文件并对下载的文件更名
 * @param {*} url
 * @param {*} filename
 */
export const downloadFile = (url, filename) => {
	function getBlob (url) {
		return new Promise(resolve => {
			const XML = new XMLHttpRequest();
			XML.open('GET', url, true);
			XML.responseType = 'blob';
			XML.onload = () => {
				if (XML.status === 200) {
					resolve(XML.response);
				}
			};
			XML.send();
		});
	}
	//下载文件
	function saveAs (blob, filename) {
		const elelink = document.createElement("a");
		elelink.style.display = 'none';
		elelink.download = filename;
		elelink.href = window.URL.createObjectURL(blob);
		document.body.appendChild(elelink);
		elelink.click();
		document.body.removeChild(elelink);
	}
	// 调用以上方法进行下载
	getBlob(url).then(blob => {
		saveAs(blob, filename);
	});
}

/**
 * 通过url下载图片
 * @param {*} img
 * @param {*} name
 */
export const downloadImage = (img,name) =>{
	var a = document.createElement('a') // 生成一个a元素
	var event = new MouseEvent('click') // 创建一个单击事件
	a.download = name || 'photo' // 设置图片名称
	a.href = img // 将生成的URL设置为a.href属性
	a.dispatchEvent(event) // 触发a的单击事件
}
